package org.mtmi.ui.controls;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.mtmi.io.IControlIO;
import org.mtmi.io.controls.SSliderIO;
import org.mtmi.ui.GraphicUtil;
import org.mtmi.ui.IMG;
import org.mtmi.ui.layouts.LayoutData;
import org.mtmi.ui.scenes.Scene;

/** 
 * Slider using image as skin.
 * <p>
 * thumb image is drawn centered on background image (vertically, or horizontally )
 */
public class SSlider extends AbstractSlider
{
	
	private String s_img_back, s_img_thumb; //needed to save control
	private Image img_back, img_thumb;
	private int thumb_length;
	
	public SSlider( Scene scene, int flags ) {
		super( scene, flags );
	}
	
	public SSlider( Scene scene, String iback, String ithumb, int valCurrent, int valMin, int valMax, int flags ) {
		super( scene, valCurrent, valMin, valMax, flags );
		setImages( iback, ithumb );
	}
	
	@Override
	public Object getAdapter( Class<?> clazz, Object arg ) {
		if( clazz == IControlIO.class ) {
			return new SSliderIO();
		}
		return super.getAdapter( clazz, arg );
	}
	
	public String getBackgroundImage() { return s_img_back; }
	public String getThumbImage() { return s_img_thumb; } 
	
	public void setImages( String iback, String ithumb ) {
		s_img_back = iback;
		s_img_thumb = ithumb;
		img_back = IMG.Get( iback );
		img_thumb = IMG.Get( ithumb );
		Rectangle r = img_thumb.getBounds();
		if( isHorizontal() ) {
			thumb_length = r.width;
		} else {
			thumb_length = r.height;
		}
		setSize( computeSize(null ) );
	}
	
	@Override
	public Point computeSize(LayoutData l) {
		Rectangle ri = img_back.getBounds();
		return new Point( ri.width, ri.height );
	}

	@Override
	public void paint(GC gc) {
		
		Rectangle r = img_back.getBounds();
		gc.drawImage( img_back, 0,0,r.width, r.height, location.x,location.y,size.x,size.y );
		
		drawThumb(gc);
	}

	private void drawThumb(GC gc) {
		int cp = getThumbPosition( getValue() );
		
		Rectangle r = img_thumb.getBounds();
		
		if( isHorizontal() ) {
			int y = location.y+(size.y-r.height)/2;
			gc.drawImage( img_thumb, cp, y );
		} else {
			int x = location.x+(size.x-r.width)/2;
			gc.drawImage( img_thumb, x, cp );
		}
	}
	
	@Override
	protected boolean isThumbContains( int ex, int ey )
	{
		Rectangle r = img_thumb.getBounds();
		int cp = getThumbPosition( getValue() );
		
		if( isHorizontal() ) {
			int y = location.y+(size.y-r.height)/2;
			return GraphicUtil.rectContains( cp, y, r.width, r.height, ex, ey );
		} else {
			int x = location.x+(size.x-r.width)/2;
			return GraphicUtil.rectContains( x, cp, r.width, r.height, ex, ey );
		}
	}

	@Override
	protected int getPMax() {
		return isHorizontal() ? location.x+size.x-thumb_length : location.y;
	}

	@Override
	protected int getPMin() {
		return isHorizontal() ? location.x : location.y+size.y-thumb_length;
	}

}
